package com.ftg.learn.javabase;

import com.ftg.learn.javabase.mapper.ShopMapper;
import com.mysql.cj.jdbc.MysqlDataSource;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import javax.sql.DataSource;

/**
 * @author 布衣
 */
public class OneClass {

    public static void main(String[] args) {
        Configuration c = new Configuration();

//         <setting name="logImpl" value="STDOUT_LOGGING"/>
        c.setLogImpl(StdOutImpl.class);


        //环境
//        数据源
//         <dataSource type="POOLED">
//                <property name="username" value="${mysql.username}"/>
//                <property name="password" value="${mysql.password}"/>
//                <property name="url" value="${mysql.url}"/>
//                <property name="driver" value="${mysql.driverClassName}"/>
//            </dataSource>
        MysqlDataSource m = new MysqlDataSource();
        m.setUser("root");
        m.setPassword("123456");
        m.setURL("jdbc:mysql://localhost:3306/framework_shop");

//          <!--        表示连接我自己本身的数据源-->
//        <environment id="kjxLocalMysql">
//            <!--        事务管理器-->
//            <transactionManager type="JDBC"/>
//            <!--        数据源-->
//            <dataSource type="POOLED">
//                <property name="username" value="${mysql.username}"/>
//                <property name="password" value="${mysql.password}"/>
//                <property name="url" value="${mysql.url}"/>
//                <property name="driver" value="${mysql.driverClassName}"/>
//            </dataSource>
//        </environment>
        Environment e = new Environment("kjxEnv", new JdbcTransactionFactory(), m);

        c.setEnvironment(e);

//        映射器
//<mappers>
//    <mapper resource="myinterface/ShopProductMapper.xml"/>
//</mappers>
        c.addMappers("com.ftg.learn.javabase.mapper");

        //////////////////////////////////////////////

        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(c);

        SqlSession sqlSession = factory.openSession();
        ShopMapper mapper = sqlSession.getMapper(ShopMapper.class);
        mapper.list();
//        springboot ++++
    }

}
